package com.shishuo.cms.tag;

import static freemarker.template.ObjectWrapper.BEANS_WRAPPER;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.shishuo.cms.constant.UserConstant;
import com.shishuo.cms.entity.UserEntity;
import com.shishuo.cms.entity.UserScanGoods;
import com.shishuo.cms.vo.GoodsItemEntityVo;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
@Service
public class GoodsItemTag extends BaseTagPlugin{
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		long goodsItemId = Long.parseLong(params.get("goodsItemId").toString());
		String redirect=(null!=params.get("type"))?params.get("type").toString():null;
		try {
			GoodsItemEntityVo GoodsItemEntityVo=goodsItemService.getVoById(goodsItemId);
			if (null!=GoodsItemEntityVo) {
				env.setVariable("m_title", BEANS_WRAPPER.wrap(GoodsItemEntityVo.getTitle()));
				env.setVariable("tag_goods_item", BEANS_WRAPPER.wrap(GoodsItemEntityVo));
			}
			HttpSession session = request.getSession();
			UserEntity userEntity=(UserEntity) session.getAttribute(UserConstant.SESSION_USER);
			if (null!=redirect&&null!=userEntity) {
				//保存此用户浏览过的产品列表
				UserScanGoods userScanGoods=new UserScanGoods();
				userScanGoods.setUserId(userEntity.getId());
				userScanGoods.setGoodsId(goodsItemId);
				userScanGoodsService.saveUserScanGoods(userScanGoods);
			}
			body.render(env.getOut());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
